<?php

namespace App\Http\Controllers\Api\Common;

use App\Http\Controllers\Controller;
use App\Models\Admin\Setting;
use App\Models\Common;
use App\Models\Member;
use App\Models\UserUni;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;

class UniController extends Controller
{
    public static function min(Request $request){
        $request->validate([
            'code' => 'required'
        ]);
        $cache = Setting::getCache();
        $param=array();
        $param[]='appid='.$cache['min_appid'];
        $param[]='secret='.$cache['min_appkey'];
        $param[]='js_code='.$request->code;
        $param[]='grant_type=authorization_code';
        $params=implode('&',$param);
        $url = Setting::MIN_URL.'?'.$params;
        $res = Http::get($url);
        $data = json_decode($res,true);
        if(!$data){
            Common::resJson([
                'code'=> 10025,
                'msg' => '微信错误',
                'data' => ''
            ]);
        }
        $unionid = isset($data['unionid'])?$data['unionid']:false;
        $useruni = UserUni::where('min_openid', $data['openid'])
            ->when($unionid,function ($query) use ($unionid){
                return $query->where('wx_unionid',$unionid);
            })->first();
        if($useruni){
            self::login($useruni);
        }else{
            UserUni::adduser('min',array_merge($request->all(),$data));
        }
        return $res;
    }

    public static function mp(){
        $cache = Setting::getCache();
    }

    public static function app(Request $request){
        $request->validate([
            'openid' => 'required',
            'logintype' => 'required'
        ]);
        $unionid = $request->input('unionid','');
        $logintype = $request->input('logintype','');
        $openid = $request->input('openid','');
        $useruni = UserUni::when($openid,function ($query) use ($logintype,$openid){
                if($logintype=='weixin'){
                    return $query->where('dev_openid',$openid);
                }else if($logintype=='qq'){
                    return $query->where('qq_openid',$openid);
                }
            })->when($unionid,function ($query) use ($unionid){
                return $query->where('qq_unionid', $unionid);
            })->first();
        if($useruni){
            self::login($useruni);
        }else{
            if($logintype=='weixin') {
                UserUni::adduser('dev',  $request->all());
            }else if($logintype=='qq'){
                UserUni::adduser('qq',  $request->all());
            }
        }
    }


    public static function login($useruni){
        if($useruni->user){
            Common::resJson([
                'code'=> 10021,
                'msg' => '登录成功',
                'data' => $useruni->user->api_login()
            ]);
        }else{
            Common::resJson([
                'code'=> 10020,
                'msg' => '用户被删除',
                'data' => ''
            ]);
        }
    }
}
